//
//  SS_TopicView.swift
//  SSNews
//
//  Created by 叶竞泽 on 2017/5/1.
//  Copyright © 2017年 叶竞泽. All rights reserved.
//

import UIKit

fileprivate let topicCellIdentifier = "TopicCell"
typealias TopicButtonClickButton = (Int) -> ()

/// 外界切换指示器,只需要更改currentButtonIndex属性即可,如需回调,需实现buttonClickBlock回调函数
class SS_TopicView: UIView {
    
    // MARK: - 属性
    
    /// 标题
    fileprivate var topicCollectionView: UICollectionView?
    
    /// 指示器
    fileprivate lazy var lineView: UIView = {
        let view = UIView(frame: CGRect.zero)
        view.backgroundColor = mainRed
        return view
    }()
    
    /// 当前选中的按钮
    var currentButtonIndex: Int? {
        didSet {
            if currentButtonIndex != nil {
                moveLineView(withIndex: currentButtonIndex!)
                buttonClickBlock?(currentButtonIndex!)
            }
        }
    }
    
    /// 回调
    var buttonClickBlock: TopicButtonClickButton?
    
    /// 标题缓存
    fileprivate let NewsTopics: [String] = ["头条", "娱乐", "社会", "科技", "时尚", "体育", "国内", "国际", "军事", "财经"]
    
    // MARK: - 监听方法
    
    fileprivate func moveLineView(withIndex index: Int) {
        UIView.animate(withDuration: 0.25) {
            self.lineView.transform = CGAffineTransform(translationX: CGFloat(index) * self.lineView.bounds.width, y: 0)
        }
    }
    
    /// 点击标题按钮执行
    @objc fileprivate func topicButtonClick(sender: UIButton) {
        
        currentButtonIndex = sender.tag
    }
    
    // MARK: - 生命周期
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setupTopicCollectionView()
    }
}

// MARK: - 设置界面
extension SS_TopicView {
    
    fileprivate func setupTopicCollectionView() {
        
        let h = bounds.height
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: h, height: h)
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        topicCollectionView = UICollectionView(frame: bounds, collectionViewLayout: layout)
        topicCollectionView?.dataSource = self
        topicCollectionView?.delegate = self
        topicCollectionView?.showsHorizontalScrollIndicator = false
        topicCollectionView?.contentSize = CGSize(width: h * CGFloat(NewsTopics.count), height: 0)
        topicCollectionView?.frame.origin.x = 0
        topicCollectionView?.frame.size.width = mainWidth
        topicCollectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: topicCellIdentifier)
        topicCollectionView?.backgroundColor = UIColor.white
        
        // FIXME: - 设置初始位置和宽度
        
        lineView.frame = CGRect(x: 0, y: bounds.height - 1, width: h, height: 1)
        topicCollectionView?.addSubview(lineView)
        addSubview(topicCollectionView!)
    }

}

// MARK: - 数据源和代理
extension SS_TopicView: UICollectionViewDataSource, UICollectionViewDelegate {

    /// cell点击
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let _ = collectionView.cellForItem(at: indexPath)
        
        // TODO: 实现点击事件
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return NewsTopics.count
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: topicCellIdentifier, for: indexPath)
        
        let button = UIButton(type: .custom)
        button.setTitle(NewsTopics[indexPath.row], for: .normal)
        button.setTitleColor(mainRed, for: .normal)
        button.contentHorizontalAlignment = .center
        button.frame = cell.bounds
        button.tag = indexPath.row
        button.addTarget(self, action: #selector(topicButtonClick(sender:)), for: .touchUpInside)
        if cell.contentView.subviews.count == 0 {
            cell.contentView.addSubview(button)
        }
        return cell
    }
    
}
